function MyPromise(fn) {
  this.success = []
  this.error = []
  this._resolve = (res) => {
    // 为了保证先收集依赖，我们将执行回调的步骤放入 宏任务队列
    setTimeout(() => {
      this.success.forEach(cb => cb(res))
    }, 0)
  }
  this._reject =  (err) => {
    setTimeout(() => {
      this.error.forEach(cb => cb(err))
    }, 0)
  }
  this.then = function (callback) {
    this.success.push(callback)
    return this
  }
  this.catch = function (cb) {
    this.error.push(cb)
    return this
  }
  fn(this._resolve, this._reject)
}

MyPromise.all = (array) => {
  let result = []
  return new MyPromise((resolve, reject) => {
    array.forEach((p, idx) => {
      p.then(res => {
        result[idx] = res
        if (result.length === array.length) {
          resolve(result)
        }
      })
        .catch(res => reject(err))
    })
  })
}

function A () {
  return new MyPromise((resolve, reject) => {
    resolve(1)
  })
}
function B () {
  return new MyPromise((resolve, reject) => {
    resolve(2)
  })
}
function C () {
  return new MyPromise((resolve, reject) => {
    resolve(3)
  })
}

MyPromise.all([A(), B(), C()]).then(res => {
  console.log(res)
})